﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WenSkin.Controls;
using static System.Windows.Forms.Control;

namespace WenFrame
{
    [ProvideProperty("InserProSqlString", typeof(Control))]
    public class PowerGetSqlString : Component, IExtenderProvider
    {
        public bool CanExtend(object extendee)
        {
            if (extendee is WenUserControl)
            {
                return true;
            }

            return false;
        }
        [Browsable(true), Category("Sql"), Description("测试编辑的内容是否显示"), DisplayName("InserProSqlString"), Localizable(true)]
        [Editor(typeof(InserProSqlStringEditor), typeof(UITypeEditor))]
        public string GetInserProSqlString(Control control)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"delete w_user_power_group where UserControlName=N'{control.Name}' \r\n");
            sb.Append($"delete w_user_power where username=N'SystemAllRoot' and UserControlName=N'{control.Name}' \r\n");

            sb.Append("/*生成所有用户都具备功能按钮权限*/\r\n");

            foreach (var item in this.RootPowers)
            {
                sb.Append("INSERT INTO [w_user_power] ([username] ,[UserControlName] ,[UserControlText] ,[PowerName] ,[PowerState]) " +
                    $"VALUES (N'SystemAllRoot' ,N'{control.Name}' ,N'{control.Text}' ,N'{item}' ,1) \r\n");
            }

            sb.Append("/*按照窗体按钮生成按钮权限*/\r\n");
            foreach (var item in control.Controls)
            {
                if (item is WenToolStrip toolStrip)
                {
                    foreach (var tool in toolStrip.Items)
                    {
                        switch (tool)
                        {
                            case ToolStripButton button:
                                {
                                    sb.Append("insert into w_user_power_group (UserControlName,UserControlText,PowerName) ");
                                    sb.Append($"values (N'{control.Name}',N'{control.Text}',N'{button.Text}') \r\n");
                                    break;
                                }

                            case ToolStripCheckBox checkBox:
                                {
                                    sb.Append("insert into w_user_power_group (UserControlName,UserControlText,PowerName) ");
                                    sb.Append($"values (N'{control.Name}',N'{control.Text}',N'{checkBox.Text}') \r\n");
                                    break;
                                }
                            case ToolStripWenComboBox comboBox:
                                {
                                    sb.Append("insert into w_user_power_group (UserControlName,UserControlText,PowerName) ");
                                    sb.Append($"values (N'{control.Name}',N'{control.Text}',N'{comboBox.PowerText}') \r\n");
                                    break;
                                } 
                            case ToolStripWenTextBox textBox:
                                {
                                    sb.Append("insert into w_user_power_group (UserControlName,UserControlText,PowerName) ");
                                    sb.Append($"values (N'{control.Name}',N'{control.Text}',N'{textBox.PowerText}') \r\n");
                                    break;
                                }
                        }
                    }
                }
            }
            return sb.ToString();
        }


        #region 私有属性

        private List<string> rootPowers;

        #endregion

        #region 公有属性

        //[Editor("System.Windows.Forms.Design.StringCollectionEditor", typeof(UITypeEditor))]
        [Category("Wen"), Description("所有用户具有权限设置")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(WenSkin.Design.Editor.StringCollectionEditor), typeof(UITypeEditor))]
        public List<string> RootPowers => rootPowers ??= new List<string>();

        #endregion

        public string AAA
        {
            get
            {
                Type type = this.GetType();
                foreach (var item in type.Assembly.DefinedTypes)
                {
                    var ssss = item.GetProperties();
                    var dd = item.GetProperty("Controls");
                    var ssd = item.GetField("Controls");
          
  
                         foreach (var pro in item.GetProperties())
                    {
                        Console.WriteLine(pro);
                    }
                } 
                //return GetInserProSqlString((this as Control).Parent);
                return "";
            }
        }

        #region 开发设计时候用
        public class InserProSqlStringEditor : UITypeEditor
        {

            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                new EditForm(this, value).ShowDialog();
                return value;
            }

            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }

            public class EditForm : WenSkin.Forms.WenForm
            {
                private WenSkin.SQL.SqlConnect SqlConnect = new WenSkin.SQL.SqlConnect(Design.Root.SqlServer, Design.Root.SqlDb, Design.Root.SqlUid, Design.Root.SqlPad);
                private InserProSqlStringEditor owner;
                private object value;
                public EditForm(InserProSqlStringEditor owner, object value)
                {
                    this.value = value;
                    this.owner = owner;
                    WenTextBox textBox = new WenTextBox()
                    {
                        Dock = DockStyle.Fill,
                        ScrollBars = ScrollBars.Vertical,
                        Multiline = true,
                        Text = value?.ToString(),
                        ReadOnly = true,
                    };

                    WenButton button = new WenButton()
                    {
                        Text = "保存数据库",
                        Dock = DockStyle.Bottom,
                    };
                    this.Controls.Add(textBox);
                    this.Controls.Add(button);
                    button.Click += Button_Click;
                }

                private void Button_Click(object sender, EventArgs e)
                {
                    if (!SqlConnect.SqlTestLook())
                    {
                        this.MsgBoxError("无法连接数据库，请设置Design.Root中的数据信息.\r\n此数据信息主要用于开发设置");
                        return;
                    }
                    SqlConnect.SqlBool(this.value?.ToString());
                    this.MsgBoxAsterisk("已成功添加到数据中");
                }
            }
        }
        #endregion

    }
}